package cn.hinglo;

import cn.hinglo.baseservice.config.DbConfig;
import cn.hinglo.baseservice.config.EhCacheConfig;
import cn.hinglo.website.bean.model.MappingKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import net.sf.ehcache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;


/**
 * @createTime: 2018/9/14 12:19
 * @author: HingLo
 * @description: 服务层的启动类 * @EnableTransactionManagement 启用事物注解 * @EnableCaching 启用缓存注解
 * * @ServletComponentScan Servlet注解 * @SpringCloudApplication
 * 定义了SpringCloud应用并包含了服务发现与服务容错保护2个注解 * @EnableFeignClients 启用Feign 客户端注解 *
 * ****************************************
 */
@EnableCaching
@SpringBootApplication
public class WebsiteServiceApplication {

    /**
     * 配置注入数据源
     */
    @Autowired
    private DataSource datasource;
    /***
     * Ehcache 的 CacheKit 工具使用
     */
    @Autowired
    private CacheManager cacheManager;

    // ************************JFinal 的ActiveRecord 配置启动**********************

    public static void main(String[] args) {
        SpringApplication.run(WebsiteServiceApplication.class, args);
        systemInit();
    }

    /***
     * 系统启动完成后标记
     */
    private static void systemInit() {
        System.out.println("=====================系统初始化成功=====================");
    }
    // ************************JFinal 的ActiveRecord 结束**********************

    // **************Ehcache 的 CacheKit 工具使用**********************

    /**
     * JFinal 的ActiveRecord 数据库依赖启动配置
     *
     * @return 返回配置对象
     */
    @Bean
    public DbConfig dbConfig() {
        ActiveRecordPlugin arp = new ActiveRecordPlugin(datasource);
        MappingKit.mapping(arp);
        return new DbConfig(arp);
    }

    @Bean
    public EhCacheConfig ehCacheConfig() {
        return new EhCacheConfig(cacheManager);
    }

}
